package com.qgy.springsecurity.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qgy.springsecurity.dao.UserDao;
import com.qgy.springsecurity.models.bases.SysUser;
import com.qgy.springsecurity.models.views.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.查询用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        SysUser sysUser = userDao.selectOne(queryWrapper);
        if (Objects.isNull(sysUser)) throw new UsernameNotFoundException("该账号不存在");
        //3.返回UserDetails对象
        return new LoginUser(sysUser);
    }
}
